package com.xt.purchasingsystem.service.impl.stockManagementServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.DFileDto;
import com.xt.purchasingsystem.dto.SPayDto;
import com.xt.purchasingsystem.mapper.DFileMapper;
import com.xt.purchasingsystem.mapper.SPayDetailsMapper;
import com.xt.purchasingsystem.mapper.SPayMapper;
import com.xt.purchasingsystem.model.DFile;
import com.xt.purchasingsystem.model.SPay;
import com.xt.purchasingsystem.model.SPayDetails;
import com.xt.purchasingsystem.service.stockManagementService.OutboundApplicationManageService;
import com.xt.purchasingsystem.utils.PageBean;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.utils.SerialNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存管理-->出库申请管理所有业务实现类
 * @author: 老姚
 * Date:  2020/5/13 14:37
 */
@Service
@Transactional
public class OutboundApplicationManageServiceImpl implements OutboundApplicationManageService {
    @Autowired
    private DFileMapper dFileMapper;

    @Autowired
    private SPayMapper sPayMapper;

    @Autowired
    private SPayDetailsMapper sPayDetailsMapper;

    @Override
    public Result getAllStorageByPage(DFileDto dFileDto) {
        int rowCount = dFileMapper.rowCountByStorage(dFileDto);
        PageBean pageBean = new PageBean(dFileDto.getPageSize(), dFileDto.getCurrNo(), rowCount);
        dFileDto.setCurrNo(pageBean.getStart());
        dFileDto.setPageSize(pageBean.getEnd());
        List<DFile> dFiles = dFileMapper.queryDFileByStorage(dFileDto);
        pageBean.setPageList(dFiles);
        return ResultFactory.buildSuccessResult("查询成功", pageBean);
    }

    @Override
    public Result selectallfile(DFileDto dFileDto) {
        List<DFile> list=dFileMapper.getAllWarehousingDfileByDto(dFileDto);
        PageHelper.startPage(dFileDto.getCurrNo(),dFileDto.getPageSize());
        PageInfo<DFile> pageInfo=new PageInfo<DFile>(list);
        return ResultFactory.buildSuccessResult("查询成功",pageInfo);
    }

    @Override
    public Result addoutboundinformation(SPayDto spaydto) {
        spaydto.getSpay().setPayId(SerialNumberUtil.outOrderSerNumber());
        sPayMapper.insert(spaydto.getSpay());
        int id=spaydto.getSpay().getId();
        for(SPayDetails sPayDetail : spaydto.getSpayDetailsList()){
            sPayDetail.setParentId(id);
            sPayDetailsMapper.insert(sPayDetail);
        };
        return ResultFactory.buildSuccessResult("提交成功,需要审核");
    }

    @Override
    public Result getAllNoCheckSpayByPage(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<SPayDto> allNoCheckSpay = sPayMapper.getAllNoCheckSpay();
        PageInfo<SPayDto> pageInfo = new PageInfo<>(allNoCheckSpay);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);

    }

    @Override
    public Result checkSpay(SPay sPay) {
        int i = sPayMapper.updateByPrimaryKeySelective(sPay);
        if (i>0){
            return ResultFactory.buildSuccessResult("审核成功");
        }else {
            return ResultFactory.buildFailResult("审核失败");
        }
    }


}
